package com.zzx.graduation.controller;



import com.zzx.graduation.config.JwtConfig;
import com.zzx.graduation.entity.GroupChat;
import com.zzx.graduation.entity.User;
import com.zzx.graduation.response.ResponseMessage;
import com.zzx.graduation.response.StateCode;
import com.zzx.graduation.service.GroupChatService;
import com.zzx.graduation.service.UserCRUD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Set;


//@Transactional
@RestController
@CrossOrigin
@Transactional
@RequestMapping("/api/group")
public class GroupChatController {

    //slf4j.Logger
    private  static final Logger log = LoggerFactory.getLogger(GroupChatController.class);

    @Autowired
    GroupChatService groupChatService;//Service


    /**
     * select 查询操作：查询一个人的所有群聊
     * 具有幂等性
     * @return
     */
    @GetMapping("/groups/{userId}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseMessage getGroups(@PathVariable("userId") Integer userId){
        List<GroupChat> groupChats = groupChatService.findGroups(userId);
        if(groupChats == null)
        {
            return  new ResponseMessage<List<GroupChat>>(StateCode.NOT_FOUNT,"查询失败",groupChats);
        }

        return   new ResponseMessage<List<GroupChat>>(StateCode.SUCCESS,"查询成功",groupChats);
    }


    /**
     * 查询一个群聊信息
     * @param id  群聊id
     * @return
     */
    @GetMapping("/group/{id}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseMessage getGroup(@PathVariable("id") Integer id){

        if(null == id){
            return  null;
        }
        HashMap<GroupChat, Set<User>> oneGroup = groupChatService.findOneGroup(id);
        if(oneGroup == null)
        {
            return  new ResponseMessage< HashMap<GroupChat, Set<User>>>(StateCode.NOT_FOUNT,"查询失败",oneGroup);
        }

        return   new ResponseMessage< HashMap<GroupChat, Set<User>>>(StateCode.SUCCESS,"查询成功",oneGroup);
    }

    /**
     * 新增一个群聊
     * @param groupChat
     * @return
     */
    @PostMapping("/group")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseMessage addGroup(@RequestBody GroupChat groupChat,Integer[] ids ){
        if(null == groupChat){
            return  null;
        }
        GroupChat chat = groupChatService.addGroup(groupChat, ids);
        if(chat == null)
        {
            return  new ResponseMessage<GroupChat>(StateCode.NOT_FOUNT,"新增失败",chat);
        }

        return   new ResponseMessage<GroupChat>(StateCode.SUCCESS,"新增成功",chat);
    }


    /**
     * 修改
     * @param groupChat
     * @return
     */
    @PutMapping("/group/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseMessage editGroup(@RequestBody GroupChat groupChat,Integer[] ids){
        if(null == groupChat){
            return  null;
        }
        GroupChat modify = groupChatService.modify(groupChat,ids);
        if(modify == null)
        {
            return  new ResponseMessage<GroupChat>(StateCode.NOT_FOUNT,"修改失败",modify);
        }

        return   new ResponseMessage<GroupChat>(StateCode.SUCCESS,"修改成功",modify);
    }

    /**
     * 删除一个群
     * 幂等性
     * 群聊id
     * */
    @DeleteMapping("/group/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public ResponseMessage deleteGroup(@PathVariable("id") Integer id){
        if(null == id){
            return  null;
        }
        Boolean b = groupChatService.deleteGroup(id);
        if(b == null || b == false)
        {
            return  new ResponseMessage<GroupChat>(StateCode.NOT_FOUNT,"删除失败",null);
        }

        return   new ResponseMessage<GroupChat>(StateCode.SUCCESS,"删除成功",null);
    }


}
